<?php

namespace Fize\Third\WechatWork\Qyapi;

use Fize\Third\WechatWork\QyapiAbstract;

/**
 * 外部联系人
 */
class Externalcontact extends QyapiAbstract
{

    /**
     * 将代开发应用或第三方应用获取的externaluserid转换成自建应用的externaluserid。
     * @param string $external_userid 服务商主体的external_userid，必须是source_agentid对应的应用所获取
     * @param int    $source_agentid  企业授权的代开发自建应用或第三方应用的agentid
     * @return array
     */
    public function fromServiceExternalUserid(string $external_userid, int $source_agentid): array
    {
        $params = [
            'external_userid' => $external_userid,
            'source_agentid'  => $source_agentid
        ];
        return $this->httpPost("/externalcontact/from_service_external_userid?access_token={$this->accessToken}", $params);
    }

    /**
     * 获取配置了客户联系功能的成员列表
     * @return array
     * @see https://developer.work.weixin.qq.com/document/path/92571
     */
    public function getFollowUserList(): array
    {
        $rst = $this->httpGet("/externalcontact/get_follow_user_list?access_token={$this->accessToken}");
        return $rst['follow_user'];
    }

    /**
     * 获取客户列表
     * @param string $userid 成员UserID
     * @return array
     * @see https://developer.work.weixin.qq.com/document/path/92113
     */
    public function getList(string $userid): array
    {
        $rst = $this->httpGet("/externalcontact/list?access_token={$this->accessToken}&userid={$userid}");
        return $rst['external_userid'];
    }

    /**
     * 获取客户详情
     * @param string      $external_userid 外部联系人的userid
     * @param string|null $cursor          上次请求返回的next_cursor
     * @return array
     * @see https://developer.work.weixin.qq.com/document/path/92114
     */
    public function get(string $external_userid, ?string $cursor = null): array
    {
        $uri = "/externalcontact/get?access_token={$this->accessToken}&external_userid={$external_userid}";
        if ($cursor) {
            $uri .= "&cursor={$cursor}";
        }
        return $this->httpGet($uri);
    }

    /**
     * 批量获取客户详情
     * @param array $params 请求参数
     * @return array
     * @see https://developer.work.weixin.qq.com/document/path/92994
     */
    public function batchGetByUser(array $params): array
    {
        return $this->httpPost("/externalcontact/batch/get_by_user?access_token={$this->accessToken}", $params);
    }

    /**
     * 修改客户备注信息
     * @param string $userid          企业成员的userid
     * @param string $external_userid 外部联系人userid
     * @param array  $params          备注参数
     * @return array
     * @see https://developer.work.weixin.qq.com/document/path/92115
     */
    public function remark(string $userid, string $external_userid, array $params): array
    {
        $params['userid'] = $userid;
        $params['external_userid'] = $external_userid;
        return $this->httpPost("/externalcontact/remark?access_token={$this->accessToken}", $params);
    }

}